package com.hex.liyue.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.hex.liyue.commonmodule.config.Constant;
import com.hex.liyue.commonmodule.utils.L;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import de.greenrobot.event.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        switch (baseReq.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            default:
                break;
        }
    }

    @Override
    public void onResp(BaseResp resp) {
        L.d("这个方法走了没有", "走了");
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                L.d("支付成功", "成功了");
                EventBus.getDefault().post(Constant.WX_SUCCESS);
                finish();
            } else if (resp.errCode == -1) {
                L.d("支付失败了", "失败了了");
                EventBus.getDefault().post(Constant.WX_FAILURE);
                finish();
            } else if (resp.errCode == -2) {
                L.d("支付取消", "取消了");
                EventBus.getDefault().post(Constant.WX_CANCEL);
                finish();
            }
        }
    }

}